<template>
  <div class="search-result-container">
    <!-- 点击实现后退效果 -->
    <van-nav-bar
      title="搜索结果"
      left-arrow
      @click-left="$router.go(-1)"
      fixed
    />

    <!-- 搜索列表结果 -->
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
      offset="20"
      :immediate-check="false"
    >
      <ZuJianItem
        v-for="item in jieGuoList"
        :key="item.art_id"
        :obj="item"
        :closeShow="false"
      ></ZuJianItem>
    </van-list>
  </div>
</template>

<script>
import ZuJianItem from '@/components/ArticleItem.vue'
import { souSuoJieGuoListAPI } from '@/api'
import { goToTime } from '@/utils/date'
export default {
  name: 'SearchResult',
  components: {
    ZuJianItem
  },
  data () {
    return {
      jieGuoList: [], // 结果列表
      loading: false, // 加载状态
      finished: false, // 是否加载完成
      page: 1 // 页码 默认为 1
    }
  },
  methods: {
    // 触底加载更多
    onLoad () {
      this.page++
      // page++ 后 再重新发起一次请求
      this.getJieGuoFn()
    },
    // 请求结果列表封装
    async getJieGuoFn () {
      const res = await souSuoJieGuoListAPI({
      // 读取页面url后拼接的路由参数
        q: this.$route.params.keywords,
        page: this.page
      })
      this.jieGuoList = [...this.jieGuoList, ...res.data.data.results]
      // console.log(res)
      if (res.data.data.results.length === 0) {
        this.finished = true
        return
      }
      res.data.data.results.forEach(obj => {
        obj.pubdate = goToTime(obj.pubdate)
      })
      this.loading = false // 加载完成改为false
    }
  },
  // 1. 一进入页面就发起一次
  created () {
    this.getJieGuoFn()
  }
}
</script>

<style lang="less" scoped>
.search-result-container {
  padding-top: 46px;
}
</style>
